/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server.boundary;

import server.entities.User;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Root;
import server.tools.MD5Hash;


/**
 *
 * @author efim
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {
    @PersistenceContext(unitName = "EFMPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }    
    
    public User createUser(Map<String,String> params)
    {
        User user = new User();
        for(String p:params.keySet())
        {
            switch(p)
            {                
                case "fname":
                    user.setFname(params.get(p));
                    break;
                case "lname":
                    user.setLname(params.get(p));
                    break;
                case "email":
                    user.setEmail(params.get(p));                   
                    break;
                case "password":
                    user.setPassword(MD5Hash.md5Java(params.get(p)));                 
                    break;                   
            }
        }
        this.create(user);
        return user;
    }
    
    public int loginUser(Map<String,String> params)
    {
        CriteriaUpdate<User> cu = getEntityManager().getCriteriaBuilder().createCriteriaUpdate(User.class); 
        Root<User> u = cu.from(User.class);        
        for(String p:params.keySet())
        {
            switch(p)
            {               
                case "email":
                    cu = cu.where(u.get("email").in(params.get(p)));                    
                    break;
                case "password":
                    cu = cu.where(u.get("password").in(MD5Hash.md5Java(params.get(p))));                    
                    break;
                case "access_token":
                    cu = cu.set("accessToken", params.get(p));     
                    break;
            }
        }                          
        return getEntityManager().createQuery(cu).executeUpdate();        
    }
    
    public List<User> getUser(Map<String,String> params)
    {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
        Root<User> u = cq.from(User.class);
        
        for(String p:params.keySet())
        {
            switch(p)
            {                
                case "id":  
                    cq = cq.where(u.get("id").in(params.get(p)));
                    break;
                case "fname":
                    cq = cq.where(u.get("fname").in(params.get(p)));
                    break;
                case "lname":
                    cq = cq.where(u.get("lname").in(params.get(p)));
                    break;
                case "email":
                    cq = cq.where(u.get("email").in(params.get(p)));                    
                    break;                
                   
            }
        }        
        TypedQuery q = getEntityManager().createQuery(cq);
        List list = q.getResultList();        
        return list;
    }
    
    public long getUserWithAccessToken(String accessToken)
    {
        long answer = -1;
        try {
            CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            Root<User> u = cq.from(User.class);
            cq = cq.where(u.get("accessToken").in(accessToken));
            cq = cq.select(u.get("id"));
            TypedQuery q = getEntityManager().createQuery(cq);
            answer = (long) q.getSingleResult();
        } catch (NoResultException ex) {
            answer = -1;
        }
        return answer;
    }
    
    
}
